#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <errno.h>  
#include "../kernel_module/myfirewall.h"

void get_status();                                          /*功能函数:获取当前防火墙过滤规则*/
void change_status(int sockfd, socklen_t len);              /*功能函数:改变防火墙过滤规则*/

ban_status rules;

int main(void){
    int sockfd;
    socklen_t len;
    if( (sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1){
        printError("create socket");
    }
    else{
        len = sizeof(rules);
        // 向内核请求当前的当前的过滤规则
        // (void*)&rules: 是一个指向缓冲区的指针，内核将把获取到的规则数据填充到这个 rules 变量中
        if (getsockopt(sockfd, IPPROTO_IP, NOWRULE, (void*)&rules, &len)){
            printError("get filtering rules from kernel space");
        }else{
            while (1)
            {
                if(rules.open_status == 1){         // 开启状态
                    get_status();                   // 循环打印当前的过滤规则
                    change_status(sockfd, len);                // 循环打印规则菜单， 直至用户选择退出
                }
            }
            
        }
    }
}

// 功能函数 获取当前防火墙过滤规则
void get_status(){
	printf("-------------------------------------------------------------------------------\n");
    if(rules.settime_status == 1){
        // 将时间戳转换为tm结构体
        struct tm start_date, end_date;
        localtime_r(&rules.start_date, &start_date);
        localtime_r(&rules.end_date, &end_date);
        printf("防火墙启用时间段：%d-%d-%d 00:00:00 ~ %d-%d-%d 23:59:59\n",start_date.tm_year + 1900
            ,start_date.tm_mon + 1,start_date.tm_mday + 1, end_date.tm_year + 1900, end_date.tm_mon + 1, end_date.tm_mday);
    }

    printf("当前防火墙功能:\n");
	printf("--------------------------------------\n");

	printf("防火墙状态检测功能: \t\t");
    if (rules.inp_status == 1){
        printf("开启\n");
    }else{
        printf("关闭");
    }
  	printf("--------------------------------------\n");

    printf("防火墙NAT功能: \t\t");
	if (rules.nat_status == 1)
	{
		printf("开启\n");
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");

    printf("根据源IP过滤功能: \t\t\t");
	if (rules.sip_status == 1)
	{
		printf("开启\n");
		for (int i = 0; i < rules.sipNum; i++)
		{
            // 转换为点分十进制打印
			printf("过滤源IP地址: %d.%d.%d.%d\n", 
			(rules.ban_sip[i] & 0x000000ff) >> 0,
			(rules.ban_sip[i] & 0x0000ff00) >> 8,
			(rules.ban_sip[i] & 0x00ff0000) >> 16,
			(rules.ban_sip[i] & 0xff000000) >> 24);
		}
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");


    printf("根据目的IP过滤功能: \t\t");
	if (rules.dip_status == 1)
	{
		printf("开启\n");
		for (int i = 0; i < rules.dipNum; i++)
		{
			printf("过滤目的IP地址: %d.%d.%d.%d\n", 
			(rules.ban_dip[i] & 0x000000ff) >> 0,
			(rules.ban_dip[i] & 0x0000ff00) >> 8,
			(rules.ban_dip[i] & 0x00ff0000) >> 16,
			(rules.ban_dip[i] & 0xff000000) >> 24);
		}
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");

    printf("根据源端口过滤功能: \t\t");
	if (rules.sport_status == 1)
	{
		printf("开启\n");
		printf("关闭端口: ");
		for (int i = 0; i < rules.sportNum; i++)
		{
			printf("%hu ", rules.ban_sport[i]);   
		}
		printf("\n");
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");

    printf("根据目的端口过滤功能: \t\t");
	if (rules.dport_status == 1)
	{
		printf("开启\n");
		printf("关闭端口: ");
		for (int i = 0; i < rules.dportNum; i++)
		{
			printf("%hu ", rules.ban_dport[i]);   
		}
		printf("\n");
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");

    printf("根据MAC过滤功能: \t\t");
	if (rules.mac_status == 1)
	{
		printf("开启\n");
		for (int i = 0; i < rules.macNum; i++)
		{
			printf("过滤MAC地址:%02X:%02X:%02X:%02X:%02X:%02X\n",
			rules.ban_mac[i][0], rules.ban_mac[i][1], rules.ban_mac[i][2], 
			rules.ban_mac[i][3], rules.ban_mac[i][4], rules.ban_mac[i][5]);
		}
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");

	printf("自定义访问控制策略功能: \t");
    if(rules.combin_status == 1){
        printf("开启\n");
        printf("共%d个自定义访问控制策略\n", rules.combineNum);
        for(int i=0;i<rules.combineNum;i++){
            printf("\n 第%d个自定义访问控制策略:\n", i+1);
            if(rules.ban_combin[i].banSip_status == 1){
                printf("源IP地址: \t%d.%d.%d.%d\n",
                (rules.ban_combin[i].banSip & 0x000000ff) >> 0,
                (rules.ban_combin[i].banSip & 0x0000ff00) >> 8,
                (rules.ban_combin[i].banSip & 0x00ff0000) >> 16,
				(rules.ban_combin[i].banSip & 0xff000000) >> 24);
            }

            if(rules.ban_combin[i].banDip_status == 1){
                printf("目的IP地址: \t%d.%d.%d.%d\n", 
				(rules.ban_combin[i].banDip & 0x000000ff) >> 0,
				(rules.ban_combin[i].banDip & 0x0000ff00) >> 8,
				(rules.ban_combin[i].banDip & 0x00ff0000) >> 16,
				(rules.ban_combin[i].banDip & 0xff000000) >> 24);
            }

            if(rules.ban_combin[i].banSport_status == 1){
                printf("源端口号: \t%hu\n", rules.ban_combin[i].ban_sport);
            }

            if(rules.ban_combin[i].banDport_status == 1){
                printf("目的端口号: \t%hu\n", rules.ban_combin[i].ban_dport);
            }

            if(rules.ban_combin[i].banMac_status == 1){
                printf("MAC地址:\t%02X:%02X:%02X:%02X:%02X:%02X\n",
				rules.ban_combin[i].banMac[0], rules.ban_combin[i].banMac[1], rules.ban_combin[i].banMac[2], 
				rules.ban_combin[i].banMac[3], rules.ban_combin[i].banMac[4], rules.ban_combin[i].banMac[5]);
            }

        }
    }else{
        printf("关闭\n");
    }
	printf("--------------------------------------\n");

    printf("关闭所有连接功能: \t\t");
	if (rules.close_status == 1)
	{
		printf("开启\n");		
	}
	else
	{
		printf("关闭\n");		
	}
	printf("--------------------------------------\n");

    printf("禁用PING功能: \t\t\t");
	if (rules.ping_status == 1)
	{
		printf("开启\n");
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");

    printf("禁用HTTP/HTTPS功能: \t\t");
	if (rules.http_status == 1)
	{
		printf("开启\n");		
	}
	else
	{
		printf("关闭\n");		
	}
	printf("--------------------------------------\n");

    printf("禁用Telnet功能: \t\t");
	if (rules.telnet_status == 1)
	{
		printf("开启\n");		
	}
	else
	{
		printf("关闭\n");		
	}
	printf("--------------------------------------\n");

    printf("根据协议类型过滤功能: \t\t");
	if (rules.protocol_status == 1)
	{
		printf("开启\n");
		printf("禁用协议: ");
		if (rules.protocol_status == 1)
		{
			if (rules.protocol_type[0])
			{
				printf("TCP ");
			}
			if (rules.protocol_type[1])
			{
				printf("UDP ");
			}
			if (rules.protocol_type[2])
			{
				printf("ICMP ");
			}
			printf("\n");
		}
	}
	else
	{
		printf("关闭\n");
	}
	printf("--------------------------------------\n");

    time_t timer0;
	timer0= time(NULL);
	struct tm* tm = localtime(&timer0);
	printf("Time:%d-%d-%d %d:%d:%d\n", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour + 8, tm->tm_min, tm->tm_sec);
}

// 功能函数： 改变防火墙过滤规则
void change_status(int sockfd, socklen_t len){
    
}